package com.customers.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.customers.entity.MainForm;
import com.customers.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/select")
public class SelectServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        String text = request.getParameter("text");
        List<MainForm> forms = null;
        try {
            CustomerService customerService = (CustomerService) request.getServletContext().getAttribute("CustomerService");
            String sql = "customerLikeName";
            forms = customerService.searchFor(text,sql);
//            forms = SelectService.searchFor(text);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询成功，返回结果
        String formjson = JSON.toJSONString(forms, JSONWriter.Feature.WriteNullStringAsEmpty);
        response.getWriter().print(formjson);
    }
}
